#include "countdown_latch.h"

CountDownLatch::CountDownLatch(unsigned int var)
: mutex_(), count_(var)
{
}

void CountDownLatch::wait()
{
    boost::mutex::scoped_lock lock(mutex_);
    while (count_ > 0) {
        condition_.wait(mutex_);
    }
}

void CountDownLatch::countDown()
{
    boost::mutex::scoped_lock lock(mutex_);
    --count_;
    if (count_ == 0) {
        condition_.notify_all();
    }
}

void CountDownLatch::countUp()
{
    boost::mutex::scoped_lock lock(mutex_);
    ++count_;
}

int CountDownLatch::getCount() const
{
    boost::mutex::scoped_lock lock(mutex_);
    return count_;
}

void CountDownLatch::setCount(int count)
{
    boost::mutex::scoped_lock lock(mutex_);
    count_ = count;
}